package param

import (
	"net"
	"strings"
)

type ParamIpv6 struct {
	Ipv6 string
}

func (i *ParamIpv6) GetKeyword() string {
	return "[ipv6]"
}

func (i *ParamIpv6) GetValue() string {
	// Test get ip
	s, err := net.InterfaceAddrs()
	if err != nil {
		panic(nil)
	}
	for _, address := range s {
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.IsGlobalUnicast() {
			if ipnet.IP.To4() == nil {
				addressStr := address.String()
				splashIndex := strings.Index(addressStr, "/")
				return addressStr[:splashIndex]
			}
		}
	}

	return ""
}
